# python和 JSON 之间的序列化与反序列化
import json
d = dict(name='Bob', age = 20, score = 88)
print(json.dumps(d))

json_str = '{"name": "Bob", "age": 20, "score": 88}'
b = json.loads(json_str)
print(b)


class Student(object):
    def __init__(self, name, age, score):
        self.name = name
        self.age = age
        self.score = score

def student2dict(std):
    return {
        'name':std.name,
        'age': std.age,
        'score': std.score
    }

s = Student('rdn', 20, 100)
json_str = json.dumps(s, default=lambda obj : obj.__dict__)
print(json_str)

def dict2student(d):
    return Student(d['name'], d['age'], d['score'])

s1 = json.loads(json_str, object_hook=dict2student)
print(s1.__str__())


import json

obj = dict(name='小明', age=20)
s = json.dumps(obj, ensure_ascii=False)
print(s)
